const app = getApp()
var hotSelectPos = 0
const wxSort = require('../../utils/wxaSortPicker.js')
const wechat = require('../../utils/wechat_commen.js')

var location
var latitude
var longtitude
Page({

  /**
   * 页面的初始数据
   */
  data: {
    messageCode: "",
    hotData: null,
    wxaSortPickerData: null,
    ishiden: false,
  },
  wxSort: wxSort,
  wechat: wechat,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getIntentData(options);
    this.connectNetForData();
  },
  getIntentData(options) {
    location = options.location;
    hotSelectPos = options.hotIndex;
    console.log(hotSelectPos)
    latitude = wechat.getStorageSync("latitude");
    longtitude = wechat.getStorageSync("lontitude")
  },

  connectNetForData() {
    var that = this;
    wx.request({
      url: app.baseNet + "city/queryCityInfo",
      method: 'POST',
      header: {
        // 'Content-Type': 'application/json'
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        console.log(res.data.cityDtos);
        var list = res.data.cityDtos;
        var hotData = list.hotcityInfoList;
        for (var i = 0; i < hotData.length; i++) {
          if (i == hotSelectPos) {
            hotData[i].isSelected = true;
          } else {
            hotData[i].isSelected = false;
          }
        }

        wxSort.init(res.data.cityDtos.cityList, that)
        that.setData({
          messageCode: res.data.messcode,
          hotData: hotData
        })
      }
    })
  },
  compareCityList(res) {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
 
  },
  search: function(e) {
    wx.navigateTo({
      url: '../firstSearch/firstSearch',
    })
  },
  /**
   * 附近房源点击事件
   */
  nearby_click: function(e) {
    var pages = getCurrentPages()
    var curPage = pages[pages.length - 1]
    var prePage = pages[pages.length - 2]
    if (typeof latitude == 'undefined' || typeof longtitude == 'undefined') {
      prePage.setLocationPermission();
    } else {
      prePage.setData({
        destinationCity: wechat.getStorageSync("location_address"),
        areaCode: '',
        cityName: '',
        latitude: wechat.getStorageSync("latitude"),
        longitude: wechat.getStorageSync("lontitude"),
        bgColor: "rgb(52,229,234)",
        color: "rgb(51,51,51)"
      })
    }
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 热门城市点击事件
   */
  click_hot: function(e) {
    var pages = getCurrentPages()
    var curPage = pages[pages.length - 1]
    var prePage = pages[pages.length - 2]
    console.log(e);
    var index = e.currentTarget.dataset.id;
    var list = this.data.hotData;
    for (var i = 0; i < list.length; i++) {
      if (i == index) {
        list[i].isSelected = true
      } else {
        list[i].isSelected = false
      }
    }
    this.setData({
      hotData: list
    })
    console.log(list)
    console.log(prePage)
    prePage.setData({
      destinationCity: list[index].name,
      cityName: list[index].name,
      areaCode: list[index].areaCode,
      hotIndex: index,
      longitude: "",
      latitude: "",
      bgColor: "rgb(52,229,234)",
      color: "rgb(51,51,51)"
    })

    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 列表的伸展和收缩
   */
  shrink_display: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.id;
    var list = this.data.wxaSortPickerData;
    for (var i = 0; i < list.length; i++) {
      var temp = list[i];
      var isShrink = temp.isShrinked;
      if (i == index) {
        temp.isShrinked = !isShrink;
      } else {
        temp.isShrinked = isShrink;
      }
      if (temp.isShrinked) {
        temp.arraw = "../../images/arraw_gray_bottom.png";
      } else {
        temp.arraw = "../../images/arraw_gray_top.png";
      }
    }
    this.setData({
      wxaSortPickerData: list
    })
  },
  click_citys: function(e) {
    var pages = getCurrentPages()
    var curPage = pages[pages.length - 1]
    var prePage = pages[pages.length - 2]
    var that = this;
    var index = e.currentTarget.dataset.id;
    var inn = e.currentTarget.dataset.listindex;
    console.log(e)
    console.log(this.data.wxaSortPickerData)
    var content = this.data.wxaSortPickerData[inn].textArray[index]
    console.log(content)
    prePage.setData({
      destinationCity: content.cityName,
      cityName: content.cityName,
      areaCode: content.areaCode,
      longitude: "",
      latitude: "",
      bgColor: "rgb(52,229,234)",
      color: "rgb(51,51,51)"
    })

    wx.navigateBack({
      delta: 1,
    })
  }


})